package cn.donghuapian.handler;

import cn.donghuapian.component.SpringBeanUtils;
import cn.donghuapian.constant.WorkflowConstant;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * @description: 网关监听器，根据网关配置的表达式判断下一个节点是哪
 * @author: hzw
 * @time: 2024/9/6 16:39
 */
public class WorkflowExecutionListener implements ExecutionListener {

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        runtimeService = (RuntimeService) SpringBeanUtils.getBean("runtimeService");
        //当前节点Key
        String activityId = delegateExecution.getCurrentActivityId();
        String processInstanceId = delegateExecution.getProcessInstanceId();
        //流程定义中设置的是1 进入审批节点 0 跳过审批节点，进入下一个网关
        switch (activityId) {
            case WorkflowConstant.GATEWAY_1:
                runtimeService.setVariable(processInstanceId, WorkflowConstant.NODE_1, 1);
                break;
            case WorkflowConstant.GATEWAY_2:
                runtimeService.setVariable(processInstanceId, WorkflowConstant.NODE_2, 1);
                break;
            case WorkflowConstant.GATEWAY_3:
                runtimeService.setVariable(processInstanceId, WorkflowConstant.NODE_3, 1);
                break;
            case WorkflowConstant.GATEWAY_4:
                runtimeService.setVariable(processInstanceId, WorkflowConstant.NODE_4, 1);
                break;
        }
    }
}
